package org.oylan.top.ctrl.admin;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by A on 2019/2/3.
 */

@Controller
@RequestMapping("/admin")
public class AdminAuthController {

    @RequestMapping("/login")
    public String login(){

        return "admin/login";
    }

    @RequestMapping(value = "/doLogin",method = RequestMethod.POST)
    public String doLogin(@RequestParam(value = "username",defaultValue = "")String username,
                          @RequestParam(value = "password",defaultValue = "")String password){

        Subject subject = SecurityUtils.getSubject();

        if(subject.isAuthenticated() == false){
            UsernamePasswordToken token = new UsernamePasswordToken(username,password);
            try{
                subject.login(token);
            }catch (Exception e){
                System.out.println(e.getMessage());
                return "admin/login";
            }
        }
        return "redirect:/admin/index";
    }
}
